const { exec } = require("../db/mysql");
const jwt = require("jsonwebtoken");
const { SuccessModel, ErrorModel } = require("../model/resModel");
const { formatStr } = require("../utils/util");

/**
 * 创建作品
 * @param {*} param
 * @param {*} token
 */
const createVideo = async (param) => {
  let { title, description, url, imgUrl, type } = param;
  console.log(description, "descript");
  const sql = `insert into video (title, description, url,imgUrl,type,createTime) values
     ("${title}","${description}","${url}","${imgUrl}",${type},${Date.now()})`;
  console.log(sql, "sql");
  const res = await exec(sql);
  if (res.affectedRows) {
    return new SuccessModel({
      data: { id: res.insertId },
      message: "创建成功",
    });
  } else {
    return new ErrorModel({
      message: "创建失败",
      httpCode: 500,
    });
  }
};
/**
 * 获取作品列表
 */
const getVideo = async (param) => {
  console.log(param, "params");
  const { id, pageSize, pageNo } = param;
  console.log(id, "id");
  // `select * from video where type=${id} limit ${(pageNo-1)*pageSize},${pageSize};`;
  const sql = `select * from video where type=${id} limit ${(pageNo - 1) * pageSize},${pageSize};`;
  const countSql = `SELECT COUNT(*) as total FROM video where type=${id}`;
  const res = await exec(sql);
  const countRes = await exec(countSql);
  return new SuccessModel({
    data: { list: res, pageSize, pageNo, total: countRes[0].total },
  });
};

const deleteVideo = async (param) => {
  const id = param.id;
  const sql = `delete from video where id = ${id}`;
  const res = await exec(sql);
  return new SuccessModel({
    message: `成功删除该视频`,
  });
};

module.exports = {
  createVideo,
  getVideo,
  deleteVideo,
};
